package com.coins.agent.zb.util;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.HttpException;

/**
 * 功能：测试调用中币接口
 *
 * @author 任梁荣
 */
public class ZBUtils {

	private static String apiUrl = "https://trade.zb.com/api";

	/**
	 * 功能：获取已开启的市场信息，包括价格、数量小数点位数
	 */
	public static String markets() throws HttpException, IOException {
		return HttpUtilManager.getInstance().requestHttpGet("http://api.zb.com/", "data/v1/markets", null);
	}

	/**
	 * 功能：获取已开启的市场信息，包括价格、数量小数点位数
	 */
	public static String ticker(String coinName) throws Exception {
		return HttpUtilManager.getInstance().requestHttpGet("http://api.zb.com/", "data/v1/ticker", String.format("market=%s", coinName));
	}

	/**
	 * 功能：获取用户信息
	 * 
	 * @throws Exception
	 */
	public static String getAccountInfo(String access, String secret) throws Exception {
		String method = "getAccountInfo";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("accesskey", access);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：获取数字资产充值记录
	 */
	public static String getChargeRecord(String access, String secret) throws HttpException, IOException {
		String method = "getChargeRecord";
		String currency = "ddm";
		String pageIndex = "1";
		String pageSize = "10";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("accesskey", access);
		params.put("currency", currency);
		params.put("pageIndex", pageIndex);
		params.put("pageSize", pageSize);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：获取数字资产提现记录
	 */
	public static String getWithdrawRecord(String coinName, String access, String secret) throws HttpException, IOException {
		String method = "getWithdrawRecord";
		String currency = "ddm";
		String pageIndex = "1";
		String pageSize = "10";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("accesskey", access);
		params.put("currency", currency);
		params.put("pageIndex", pageIndex);
		params.put("pageSize", pageSize);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：获取用户认证的提现地址
	 */
	public static String getWithdrawAddress(String access, String secret) throws HttpException, IOException {
		String method = "getWithdrawAddress";
		String currency = "ddm";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("accesskey", access);
		params.put("currency", currency);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：获取用户充值地址
	 */
	public static String getUserAddress(String access, String secret) throws HttpException, IOException {
		String method = "getUserAddress";
		String currency = "ddm";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("accesskey", access);
		params.put("currency", currency);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：获取未成交或部份成交的买单和卖单
	 * 
	 * @throws IOException
	 * @throws HttpException
	 */
	public static String getUnfinishedOrdersIgnoreTradeType(String access, String secret) throws HttpException, IOException {
		String method = "getUnfinishedOrdersIgnoreTradeType";
		String currency = "ddm_qc";
		String pageIndex = "1";
		String pageSize = "10";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("accesskey", access);
		params.put("currency", currency);
		params.put("pageIndex", pageIndex);
		params.put("pageSize", pageSize);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：获取自己历史委托（新）
	 */
	public static String getOrdersIgnoreTradeType(String coinName, String pageIndex, String pageSize, String access, String secret)
			throws HttpException, IOException {
		String method = "getOrdersIgnoreTradeType";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("currency", coinName);
		params.put("accesskey", access);
		params.put("pageIndex", pageIndex);
		params.put("pageSize", pageSize);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：获取自己历史委托（新）
	 * 
	 * @throws IOException
	 * @throws HttpException
	 */
	public static String getOrdersNew(String coinName, String pageIndex, String pageSize, String tradeType, String access, String secret)
			throws HttpException, IOException {
		String method = "getOrdersNew";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("tradeType", tradeType);
		params.put("currency", coinName);
		params.put("accesskey", access);
		params.put("pageIndex", pageIndex);
		params.put("pageSize", pageSize);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：获取自己历史委托(旧)
	 * 
	 * @throws IOException
	 * @throws HttpException
	 */
	public static String getOrders(String access, String secret) throws HttpException, IOException {
		String method = "getOrders";
		String currency = "ddm_qc";
		String tradeType = "1";
		String pageIndex = "1";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("tradeType", tradeType);
		params.put("currency", currency);
		params.put("accesskey", access);
		params.put("pageIndex", pageIndex);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：获取委托单
	 * 
	 * @throws IOException
	 * @throws HttpException
	 */
	public static String getOrder(String access, String secret, String coinName, String entrustCode) throws HttpException, IOException {
		String method = "getOrder";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("id", entrustCode);
		params.put("currency", coinName);
		params.put("accesskey", access);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：取消委托单
	 * 
	 * @throws IOException
	 * @throws HttpException
	 */
	public static String cancelOrder(String access, String secret) throws HttpException, IOException {
		String method = "cancelOrder";
		String id = "20180207955012";
		String currency = "ddm_qc";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("id", id);
		params.put("currency", currency);
		params.put("accesskey", access);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}

	/**
	 * 功能：委托下单(tradeType 交易类型1/0[buy/sell])
	 */
	public static String order(String tradeType, String price, String amount, String coinName, String access, String secret) throws HttpException, IOException {
		String method = "order";
		Map<String, String> params = new TreeMap<String, String>();
		params.put("method", method);
		params.put("price", price);
		params.put("amount", amount);
		params.put("tradeType", tradeType);
		params.put("currency", coinName);
		params.put("accesskey", access);
		String digest = DigestUtils.digest(secret);
		String sign = DigestUtils.hmacSign(MapSort.toStringMap(params), digest);
		params.put("sign", sign);
		params.put("reqTime", String.valueOf(System.currentTimeMillis()));
		return HttpUtilManager.getInstance().requestHttpGet(String.format("%s/%s", apiUrl, method), params);
	}
}
